<template>
    <view class="poster">
        <image :src="sharePath1" style="width: 100%;height: 100%;" mode="widthFix"></image>
        <view v-if="isShowBtn" class="btn" @click="goHome" >进入首页</view>
    </view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad,onShareAppMessage,onShareTimeline } from '@dcloudio/uni-app'
import { getUserInfo } from '@/utils/user'
import { getSharePathApi } from '@/api/index'
//分享人id
const shareUserId = ref('') 
let isLogin = ref(false)
let userInfo = ref({})
let isShowBtn = ref(false)

// 获取用户信息
const getUser = async () => {
    isLogin.value = uni.getStorageSync('isLogin')
    console.log('isLogin-----------------',isLogin.value)
    if (!isLogin.value) {
        //跳转登录
        uni.reLaunch({
            url: '/pages/Login/Login?lastPage=pages/poster/index&params=' + JSON.stringify({shareUserId:shareUserId.value})
        })
        return
    }
    const res = await getUserInfo()
    console.log('获取用户信息',res)
     //获取本地用户信息
    if (res.code === 200) {
        userInfo.value = res.result
        userInfo.value.phone = userInfo.value.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
    }
}

const goHome = () => {
    uni.switchTab({
        url: '/pages/index/index'
    })
}

onShareAppMessage(()=>{
    
    return {
        title: '玖盛谷',
        desc: '玖盛谷',
        imageUrl: sharePath1.value,
        path: '/pages/poster/index?shareUserId=' + userInfo.value.userId
    }

    // return{
    //     title:'测试分享标题',
    //     desc:'我是内容',
    //     imageUrl:'https://cp1.douguo.com/upload/caiku/a/b/2/yuan_abf417888eb0929d7f5a3e1cc01aab92.jpeg'
    // }
})

//分享朋友圈
onShareTimeline(()=>{
    return {
        title: '玖盛谷',
        desc: '玖盛谷',
        imageUrl: sharePath1.value,
        path: '/pages/poster/index?shareUserId=' + userInfo.value.userId
    }

})

const sharePath1 = ref('')

onLoad((options) => {
    console.log('海报页面参数',options)
    // 判断是否是登录页面跳转过来的

    //params = 登录页面
    //shareUserId = 链接分享
    //invitationCode = 二维码


    if(options.params){ 
        shareUserId.value = JSON.parse(options.params)
    }else{
        shareUserId.value = options.shareUserId || options.invitationCode
    }
    if(shareUserId.value){
        isShowBtn.value = true
    }
    getSharePathApi().then(res=>{
        console.log('获取分享路径',res)
        sharePath1.value = res.result.image2
    })
    getUser()
})
</script>
<style scoped>
.poster{
    width: 100%;
    min-height: 100vh;
    /* height: 100%; */
}
.btn{
    position: fixed;
    z-index: 10;
    bottom: 100rpx;
    left: 50%;
    transform: translate(-50%,-50%);
    width: 690rpx;
    height: 92rpx;
    background: #001C71;
    border-radius: 100rpx 100rpx 100rpx 100rpx;
    color: #fff;
    font-size: 32rpx;
    text-align: center;
    line-height: 92rpx;
}
</style>
